#! /bin/bash

#    Copyright (C) 2007-2009  Ludovic Rousseau  <ludovic.rousseau@free.fr>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
#    02110-1301 USA.

# $Id: configure 6264 2012-04-06 15:45:54Z rousseau $

# to use
# ./MacOSX/configure
# make
# make install
# the driver is installed in /usr/libexec/SmartCardServices/drivers

# Colors
RED="\033[31m"
NORMAL="\033[0m"

# run this script as ./MacOSX/configure to configure for Mac OS X
if [ ! -d MacOSX ]
then
	echo -e $RED
	echo "ERROR!"
	echo "run ./MacOSX/configure from the source top directory"
	echo -e $NORMAL
	exit;
fi

# find pcsc-lite header files in MacOSX/
# use ${varname:-word} to return word only if varname is not already defined
PCSC_CFLAGS=${PCSC_CFLAGS:--I$(pwd)/MacOSX}
PCSC_LIBS=${PCSC_LIBS:--framework PCSC}

# use libusb-1.0
LIBUSB_ARCHIVE=/usr/local/lib/libusb-1.0.a
LIBUSB_CFLAGS=-I/usr/local/include/libusb-1.0
LIBUSB_LIBS="$LIBUSB_ARCHIVE -Wl,-framework -Wl,IOKit -Wl,-framework -Wl,CoreFoundation -lobjc"

# RESPONSECODE is already defined by PCSC/wintypes.h
# define needed here to compile examples/scardcontrol.c since config.h is
# not included
CFLAGS="$CFLAGS -DRESPONSECODE_DEFINED_IN_WINTYPES_H"

# Build a Universal Binary
UB=$(file $LIBUSB_ARCHIVE | grep "Mach-O universal binary")
echo $UB
if [ -z "$UB" ]
then
	echo "*************************"
	echo "No Universal Binary build"
	echo "*************************"
else
	echo "Universal Binary build"
	CFLAGS="$CFLAGS -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk -arch i386 -arch x86_64"
fi
echo

CONFIGURE_ARGS="--disable-dependency-tracking"

# do not build a static driver
# (building fails when linking statically with libusb)
CONFIGURE_ARGS="$CONFIGURE_ARGS --disable-static"

# do not use pcscd debug feature
CONFIGURE_ARGS="$CONFIGURE_ARGS --disable-pcsclite"

# simulate a composite device as multi slots
CONFIGURE_ARGS="$CONFIGURE_ARGS --enable-composite-as-multislot"

# use a specific bundle name to NOT overwrite the official CCID driver
#CONFIGURE_ARGS="$CONFIGURE_ARGS --enable-bundle=ifd-ccid-foobar.bundle"

set -x
./configure \
	CFLAGS="$CFLAGS" \
	PCSC_CFLAGS="$PCSC_CFLAGS" \
	PCSC_LIBS="$PCSC_LIBS" \
	LIBUSB_CFLAGS="$LIBUSB_CFLAGS" \
	LIBUSB_LIBS="$LIBUSB_LIBS" \
	LDFLAGS="$LDFLAGS" \
	--enable-usbdropdir=/usr/libexec/SmartCardServices/drivers \
	$CONFIGURE_ARGS \
	"$@"

# force a regeneration of Info.plist
rm -f src/Info.plist

